/*
 *  SyncIT , easy synchronization of files between computers and devices.
 *  Copyright (C) 2011  Christian Masus <cmasus(at)gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version. If redistributing and/or modifying
 *  this file, this notice must be included in the redistributed or
 *  modified copy.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui.javagui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;

@SuppressWarnings("serial")
public class SyncTableFolderRenderer extends JPanel implements TableCellRenderer {

	private JLabel textField;
	private JButton button;
	private Color buttonColor;

	public SyncTableFolderRenderer() {
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		setOpaque(true);

		textField = new JLabel();
		Font font = textField.getFont();
		font = font.deriveFont(Font.PLAIN);
		textField.setFont(font);
		textField.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		button = new JButton(" ... ");
		button.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		buttonColor = button.getBackground();

		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.9;
		add(textField, c);
		c.gridx = 1;
		c.gridy = 0;
		c.weightx = 0.0;
		c.ipady = -2;
		c.fill = GridBagConstraints.NONE;
		add(button, c);
	}

	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		String text = (String)value;
		if(text == null || text.equals("")) {
			text = " ";
		}
		textField.setText(text);

		if(isSelected) {
			setBackground(table.getSelectionBackground());
			textField.setBackground(table.getSelectionBackground());
			button.setBackground(table.getSelectionBackground());
		}
		else {
			if(row % 2 == 1) {
				setBackground(new Color(243, 249, 255)); // Light blue
				textField.setBackground(new Color(243, 249, 255)); // Light blue
			}
			else {
				setBackground(table.getBackground());
				textField.setBackground(table.getBackground());
			}
			button.setBackground(buttonColor);
		}

		if(hasFocus) {
			Border border = null;
			if(isSelected) {
				border = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
			}
			if(border == null) {
				border = UIManager.getBorder("Table.focusCellHighlightBorder");
			}
			setBorder(border);
		}
		else {
			setBorder(new EmptyBorder(1, 1, 1, 1));
		}

		return this;
	}

	public int getButtonWidth() {
		Dimension dim = button.getSize();
		return dim.width;
	}
}
